<?php

/**
 * This is the model class for table "measurements_channels".
 *
 * The followings are the available columns in table 'measurements_channels':
 * @property integer $id
 * @property integer $measurementId
 * @property integer $channelSequence
 * @property integer $value
 */
class MeasurementChannel extends ActiveRecord {

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return MeasurementChannel the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'measurements_channels';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		$rules = parent::rules();
		$rules[] = array(
			array(
				'measurementId',
				'channelSequence',
				'value',
			),
			'numerical',
			'integerOnly'=>true,
		);
		$rules[] = array(
			array(
				'id',
				'measurementId',
				'channelSequence',
				'value',
				'createdAt',
				'updatedAt',
			),
			'safe',
			'on'=>'search',
		);
		return $rules;
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		$relations = parent::relations();
		$relations['channel'] = array(
			self::BELONGS_TO,
			'Channel',
			array('channelSequence'=>'sequence'),
		);
		return $relations;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id'=>'ID',
			'measurementId'=>'Measurement',
			'channelSequence'=>'Channel Sequence',
			'value'=>'Raw Value',
			'cuteValue'=>'Value',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models
	 *   based on the search/filter conditions.
	 */
	public function search() {
		$criteria = new CDbCriteria;
		$criteria->compare('id', $this->id);
		$criteria->compare('measurementId', $this->measurementId);
		$criteria->compare('channelSequence', $this->channelSequence);
		$criteria->compare('value', $this->value);
		$criteria->compare('createdAt', $this->createdAt, true);
		$criteria->compare('updatedAt', $this->updatedAt, true);
		return new CActiveDataProvider($this, array('criteria'=>$criteria));
	}
	
	public function getCuteValue() {
		$value = $this->value;
		
		if (!$this->channel) return $value;
		
		if (!$this->channel->rule) return $value;
		
		return eval($this->channel->rule);
	}

}